package com.lzm.ydpt.module.mall.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.byt.framlib.base.BaseFragment;
import com.byt.framlib.baseadapter.rv.RvCommonAdapter;
import com.byt.framlib.baseadapter.rv.RvViewHolder;
import com.byt.framlib.basemvp.BasePresenter;
import com.byt.framlib.commonwidget.LoadingTip;
import com.byt.framlib.commonwidget.NormalTitleBar;
import com.byt.framlib.commonwidget.OnNoDoubleClickListener;
import com.byt.framlib.commonwidget.onclicktwo.AntiShake;
import com.byt.framlib.commonwidget.refresh.RefreshHeaderView;
import com.lzm.ydpt.R;
import com.lzm.ydpt.entity.mall.MallAddress;
import com.lzm.ydpt.entity.mall.ShopInfoBean;
import com.lzm.ydpt.module.mall.activity.SelectLocationActivity;
import com.lzm.ydpt.module.mall.activity.ShopInfoDetailActivity;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * 商城附近商检
 */
public class MallNearbyShopFragment extends BaseFragment {
    private static MallNearbyShopFragment sFragment;
    @BindView(R.id.ntb_mall_shop_nearby)
    NormalTitleBar ntb_mall_shop_nearby;
    @BindView(R.id.tv_nearby_location_address)
    TextView tv_nearby_location_address;
    @BindView(R.id.tv_location_type)
    TextView tv_location_type;
    @BindView(R.id.ltp_mall_nearby_shop)
    LoadingTip ltp_mall_nearby_shop;
    @BindView(R.id.srf_mall_nearby_shop)
    SmartRefreshLayout srf_mall_nearby_shop;
    @BindView(R.id.rv_mall_nearby_shop)
    RecyclerView rv_mall_nearby_shop;
    private List<ShopInfoBean> shopInfoBeans = new ArrayList<>();
    private RvCommonAdapter<ShopInfoBean> adapter = null;
    public final static int address_code = 0x009; // 选择地址

    public static MallNearbyShopFragment newsInstance() {
        sFragment = new MallNearbyShopFragment();
        return sFragment;
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_mall_nearby_shop;
    }

    @Override
    public BasePresenter initPresenter() {
        return null;
    }

    @Override
    protected void initView() {
        ntb_mall_shop_nearby.setBackVisibility(false);
        ntb_mall_shop_nearby.setTitleText("附近商家");
        intiRefrsh();
        initAdapter();
    }

    private void intiRefrsh() {
        setRefreshLayout(srf_mall_nearby_shop);
        srf_mall_nearby_shop.setEnableLoadMore(false);
        srf_mall_nearby_shop.setEnableRefresh(true);
        srf_mall_nearby_shop.setRefreshHeader(new RefreshHeaderView(context).getHeaderStyleStaffWhite());
        srf_mall_nearby_shop.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {

            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {

            }
        });
    }

    private void initAdapter() {
        rv_mall_nearby_shop.setLayoutManager(new LinearLayoutManager(context));
        adapter = new RvCommonAdapter<ShopInfoBean>(context, shopInfoBeans, R.layout.item_mall_nearby_shop_rv) {
            @Override
            protected void convert(RvViewHolder holder, ShopInfoBean shopInfoBean, int position) {
//                ImageLoaderUtils.displayCircle(holder.getView(R.id.img_shop_ablum),);
//                holder.setText(R.id.tv_shop_name,);
//                holder.setText(R.id.tv_shop_follow_num,"关注");
//                holder.setText(R.id.tv_shop_buy_num,"已售");
//                holder.setText(R.id.tv_shop_distance_num,"距离");
                holder.setOnClickListener(R.id.tv_shop_nearby_follow, new OnNoDoubleClickListener() {
                    @Override
                    protected void onNoDoubleClick(View v) {
                        openActivity(ShopInfoDetailActivity.class);
                    }
                });
//                ImageLoaderUtils.display(holder.getView(R.id.img_show_shop_01),);
//                ImageLoaderUtils.display(holder.getView(R.id.img_show_shop_02),);
//                ImageLoaderUtils.display(holder.getView(R.id.img_show_shop_03),);
//                holder.setText(R.id.tv_show_price_01,"距离");
//                holder.setText(R.id.tv_show_price_02,"距离");
//                holder.setText(R.id.tv_show_price_03,"距离");

            }
        };
        rv_mall_nearby_shop.setAdapter(adapter);
    }

    @OnClick({R.id.tv_location_nearby_select})
    public void onClick(View view) {
        if (AntiShake.check(view.getId())) {
            return;
        }
        switch (view.getId()) {
            case R.id.tv_location_nearby_select:
                Bundle bundle = new Bundle();
                bundle.putInt(SelectLocationActivity.INP_LOCATION_TYPE, 1);
                openActivityForResult(SelectLocationActivity.class, bundle, address_code);
                break;
        }
    }

    @Override
    public void onDestroyView() {
        sFragment = null;
        super.onDestroyView();
    }

    public void onChangeAddress(Intent data) {
        MallAddress mallAddress = data.getParcelableExtra(SelectLocationActivity.INP_LOCATION_ADDRESS);
    }
}
